unit clsObsPedVenda;

interface

uses Dm, dbtables, biblio, Sysutils;

type
  TObsPedVenda = class(TObject)
  private
    Ped_numero: string;
  public
    Obsped_1: string;
    Obsped_2: string;
    Obsped_3: string;
    Obsped_4: string;
    Obsped_5: string;
    Obsped_6: string;
    Obsped_7: string;
    function Gravar(p_Ped_numero: string): Byte;
    function Recuperar(p_ped_1, P_Ped_2: string): Byte;
    function Excluir(P_Ped_1, P_ped_2: string): Byte;
    //      procedure Consultar(var p_q:TQuery; p_Pro_codigo, p_desc:String);
  end;

implementation

function TObsPedVenda.Excluir(P_Ped_1, P_ped_2: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dm.dmint);
  tq.databasename := dm.dmint.db.databasename;
  tq.name := 'qObsPedVenda';
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM ObsPedVenda WHERE Ped_numero=:k_Ped_1 or Ped_numero=:K_Ped_2');
  tq.parambyname('k_Ped_1').asstring := strzero(P_Ped_1, 9);
  tq.parambyname('k_Ped_2').asstring := strzero(P_ped_2, 9);
  try
    tq.execSQL;
    Result := 1;
  except
    result := 0;
  end;
  tq.close;
  tq.Free;
end;

function TObsPedVenda.Gravar;

var
  tq: TQuery;

begin
  tq := Tquery.Create(dm.DmInt);
  tq.databasename := dm.dmint.Db.DatabaseName;
  tq.name := 'qObsPedVenda';
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM ObsPedVenda where Ped_numero=:k_Ped_numero');
  tq.parambyname('k_Ped_numero').asstring := strzero(p_Ped_numero, 9);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO ObsPedVenda VALUES(' +
      ':k_Ped_numero,' +
      ':k_Obsped_1,' +
      ':k_Obsped_2,' +
      ':k_Obsped_3,' +
      ':k_Obsped_4,' +
      ':k_Obsped_5,' +
      ':k_Obsped_6,' +
      ':k_Obsped_7)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE ObsPedVenda SET ' +
      'Obsped_1 =:k_Obsped_1,' +
      'Obsped_2 =:k_Obsped_2,' +
      'Obsped_3 =:k_Obsped_3,' +
      'Obsped_4 =:k_Obsped_4,' +
      'Obsped_5 =:k_Obsped_5,' +
      'Obsped_6 =:k_Obsped_6,' +
      'Obsped_7 =:k_Obsped_7 ' +
      'WHERE Ped_numero=:k_Ped_numero');
  end;
  tq.parambyname('k_Ped_numero').asstring := strzero(p_Ped_numero, 9);
  tq.parambyname('k_Obsped_1').asstring := ObsPed_1;
  tq.parambyname('k_Obsped_2').asstring := ObsPed_2;
  tq.parambyname('k_Obsped_3').asstring := ObsPed_3;
  tq.parambyname('k_Obsped_4').asstring := ObsPed_4;
  tq.parambyname('k_Obsped_5').asstring := ObsPed_5;
  tq.parambyname('k_Obsped_6').asstring := ObsPed_6;
  tq.parambyname('k_Obsped_7').asstring := ObsPed_7;
  tq.close;
  try
    tq.execsql;
    result := 1;
  except
    result := 0;
  end;
  tq.Free;
end;

function TObsPedVenda.Recuperar(p_ped_1, P_Ped_2: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dm.dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM ObsPedVenda where Ped_numero=:K_Ped_1 or Ped_numero=:K_ped_2 order by ped_numero');
  tq.Params[0].asstring := strzero(p_ped_1, 9);
  tq.Params[1].asstring := strzero(p_ped_2, 9);
  tq.open;
  if tq.eof then begin
    ObsPed_1 := '';
    ObsPed_2 := '';
    ObsPed_3 := '';
    ObsPed_4 := '';
    ObsPed_5 := '';
    ObsPed_6 := '';
    ObsPed_7 := '';
    Result := 0;
  end else begin
    Ped_numero := tq.fieldbyname('Ped_numero').asstring;
    Obsped_1 := tq.fieldbyname('Obsped_1').asstring;
    Obsped_2 := tq.fieldbyname('Obsped_2').asstring;
    Obsped_3 := tq.fieldbyname('Obsped_3').asstring;
    Obsped_4 := tq.fieldbyname('Obsped_4').asstring;
    Obsped_5 := tq.fieldbyname('Obsped_5').asstring;
    Obsped_6 := tq.fieldbyname('Obsped_6').asstring;
    Obsped_7 := tq.fieldbyname('Obsped_7').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

end.
